package CutLoad.transitions.cloud
{
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	import CutLoad.transitions.Easing;
	import CutLoad.transitions.Record;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	/**
	 * 缓动向下移动，或向下移动   需要注意：这里需要逐帧调用enterFrame()方法，效果方可生效
	 * @author hai
	 * 
	 */	
	public class EasingUpDown
	{
		//目标
		private var target:DisplayObject;
		private var moveTarget:DisplayObject
		//
		private var startNum:Number,endNum:Number;
		private var isMoveY:Boolean;
		//缓动效果
		private var easing:Easing;
		private var record:Record;
		//切换状态
		private var statesUpDown:Boolean;
		//是否正在播放
		private var isPlay:Boolean;
		/**
		 * 初始化 
		 * @param target 要操的目标 注意：它的位置就是出现地位置
		 * @param startNum 消失的地方
		 * @param isMoveY 取值为:true:上下运动  false:左右移动
		 * @param endSet 缓动与目标相差多少停下来。  默认:1
		 * @param offset 缓动系数,取值范围0~1 默认为0.2;
		 * 
		 */		
		public function EasingUpDown(target:DisplayObject,startNum:Number,isMoveY:Boolean=true,endSet:Number=1,offset:Number=0.2,isPhoto:Boolean=false)
		{
			if(target.parent==null) throw new Error("target.parent==null!");
			this.target=target;
			this.isMoveY=isMoveY;
			this.startNum=startNum;
			if(isMoveY){
				this.endNum=target.y;
			}else{
				this.endNum=target.x;
			}
			//
			//拍照
			if(isPhoto){
				moveTarget=Painter.getImg(target);
				target.visible=false;
			}else{
				moveTarget=target;
			}
			//
			if(isMoveY){
				moveTarget.x=target.x;
				moveTarget.y=startNum;
			}else{
				moveTarget.y=target.y;
				moveTarget.x=startNum;
			}
			//
			if(isPhoto) target.parent.addChild(moveTarget);
			//
			easing=new Easing(this.startNum,this.endNum,endSet,offset);
			easing.setFunEnterFrame(function(num:Number):void{
				if(isMoveY){
					moveTarget.y=num;
				}else{
					moveTarget.x=num;
				}
			});
			easing.setFunEnd(function(num:Number):void{
				if(isMoveY){
					moveTarget.y=num;
				}else{
					moveTarget.x=num;
				}
				moveTarget.visible=false;
				target.visible=true;
				statesUpDown=false;
				isPlay=false;
			});
			record=easing.getRecord();
			record.setFunEnterFrame(function(num:Number):void{
				if(isMoveY){
					moveTarget.y=num;
				}else{
					moveTarget.x=num;
				}
			});
			record.setFunEnd(function(num:Number):void{
				if(isMoveY){
					moveTarget.y=num;
				}else{
					moveTarget.x=num;
				}
				statesUpDown=true;
				isPlay=false;
			});
			record.setIsAd(false);
			//
			statesUpDown=true;
			isPlay=false;
		}
		/**
		 * 现在是否处于伸出的状态 
		 * @return 
		 * 
		 */		
		public function isUpState():Boolean{
			return !statesUpDown
		}
		public function start():void{
			if(isPlay) return;
			isPlay=true;
			if(statesUpDown){
				easing.start();
			}else{
				target.visible=false;
				moveTarget.visible=true;
				record.start();
			}
		}
		public function enterFrame():void{
			if(!isPlay) return;
			if(statesUpDown){
				easing.enterFrame();
			}else{
				record.enterFrame();
			}
		}
	}
}